import reshandler from 'services/response-handler'
import getLocation from "services/get-location";
import Vue from 'Vue'
/*
 * 查询推荐服务列表
 */
//function getTime() {
//  var d = new Date();
//  var yy = d.getFullYear();
//  var mm = d.getMonth() + 1;
//  var dd = d.getDate();
//  var hh = d.getHours();
//  var MM = d.getMinutes();
//  var ss = d.getSeconds();
//  var time = yy + "-" + (mm > 9 ? mm : '0' + mm) + "-" + (dd > 9 ? dd : '0' + dd) + " " + hh + ":" + MM + ":" + ss;
//  return time
//}

function timeFunction(time){
	let date
	let which = ''
	let now = new Date()
	console.log(navigator.userAgent)
	if (/iPhone|iPad|iPod/i.test(navigator.userAgent) || navigator.userAgent.indexOf("Safari") > 0 ) {
		date = new Date(time.replace(/-/g, "/"));  
	} else {
		date = new Date(time)
	}
	if(date.getFullYear() === now.getFullYear() && date.getDate() !== now.getDate()) which = 'year'
	else if(date.getFullYear() === now.getFullYear() && date.getDate() === now.getDate()) which = 'today'
	else which = 'other'
	let timeFilter = {
		today:function(time){
			return time.slice(time.length-8,time.length-3)
		},
		year:function(time){
			return time.slice(5,10)
		},
		other:function(time){
			return time.slice(0,7)
		}
	}
	return timeFilter[which](time)
}

function makeData(res) {
	let resArr = []
    let list = res.recommendList || res.followList
    list.forEach(val =>{
//  	console.log(val)
    	
    	let specials = ""
    	let imageArr = []
    	let hasImage = false
		let data =  {
			page:'service',
			attendCount:val.attendCount?val.attendCount:'',
			content:val.content?val.content:'',
			followCount:val.followCount?val.followCount:'',
			id:val.id?val.id:'',
			attendNum: val.attendCount ? (val.attendCount === '' ? 0 : val.attendCount) : '0',
			publishDate:val.publishDate?timeFunction(val.publishDate):'',
			shop:val.shop?{
				address:val.shop&&val.shop.address?val.shop.address:'',
				creditRate:val.shop&&val.shop.creditRate?val.shop.creditRate:'',
				id:val.shop&&val.shop.id?val.shop.id:'',
				latitude:val.shop&&val.shop.latitude?val.shop.latitude:'',
				longitude:val.shop&&val.shop.longitude?val.shop.longitude:'',
				longitude:val.shop&&val.shop.longitude?val.shop.longitude:'',
				mobile:val.shop&&val.shop.mobile?val.shop.mobile:'',
				name:val.shop&&val.shop.name?val.shop.name:'',
				specialServices:''
			}:'',
			title:val.title?val.title:'',
			type:val.type?val.type:'',
			images:val.images&&val.images.length>0?val.images:'',
			vr:val.vr?{
				filePath:val.vr.filePath?val.vr.filePath:'',
				id:val.vr.id?val.vr.id:''
			}:'',
			promotion:val.promotion?{
				id:val.promotion.id?val.promotion.id:'',
				type:val.promotion.type?val.promotion.type:'',
				state:val.promotion.state?val.promotion.state:''
			}:'',
			article:val.article?{
				id:val.article.id?val.article.id:'',
				path:val.article.path?val.article.path:'',
			}:'',
			ad:val.ad?{
				id:val.ad.id?val.ad.id:'',
				path:val.ad.path?val.ad.path:'',
			}:'',
			marketing:val.marketing?{
				id:val.marketing.id?val.marketing.id:'',
				type:val.marketing.type?val.marketing.type:'',
				state:val.marketing.state?val.marketing.state:''
			}:'',
			productList:val.productList?{
				id:val.productList.id?val.productList.id:'',
				title:val.productList.fullName?val.productList.fullName:'',
				price:val.productList.price?val.productList.price:'',
				image:val.productList.image?val.productList.image:'',
			}:'',
			forum_type:val.vr || val.promotion || val.article || val.ad || val.marketing || val.productList || ''
		}
//data工厂 特色服务
		val.shop&&val.shop.specialServices&&val.shop.specialServices.length>0&&val.shop.specialServices.forEach((item,index)=>{
//			console.log(item)
			specials += item + '|'
		})
//data工厂 图片		
		val.productList&&val.productList.length>0&&val.productList.forEach((item,index)=>{
//			console.log(item.images)
			imageArr.push(item.image)
			hasImage = true
		})
		data.shop.specialServices = specials.slice(0,specials.length-1)
		if(hasImage == true) data.images = imageArr
		resArr.push(data)
	})
    return resArr
//  for (let i = 0; i < response.recommendList.length; i++) {
//      let services = ''
//      let publishDate = response.recommendList[i].publishDate
//      if (publishDate.substring(0, 10) === nowTime.substring(0, 10)) {
//          response.recommendList[i].showTime = publishDate.substring(10, 16)
//      } else if (publishDate.substring(0, 4) !== nowTime.substring(0, 4)) {
//          response.recommendList[i].showTime = publishDate.substring(0, 10)
//      } else {
//          response.recommendList[i].showTime = publishDate.substring(5,10)
//      }
//      for (let j = 0; j < response.recommendList[i].shop.specialServices.length; j++) {
//          if (j != response.recommendList[i].shop.specialServices.length - 1) {
//              services = services + response.recommendList[i].shop.specialServices[j] + '|'
//          } else {
//              services = services + response.recommendList[i].shop.specialServices[j]
//          }
//      }
//      response.recommendList[i].shop.specialServices = services;
//  }
//  return Object.assign({}, response.recommendList, {
//  })
}
export default function (params, success, fail,routers) {
    // request 
    params = {
    	'fork' : Object.assign({}, params, {
			        methodName: "ListRecommendFollow",
			    }),
		'commend' : Object.assign({}, params, {
			        methodName: "ListRecommend",
			    }),
    }
//  params = Object.assign({}, params, {
//      methodName: "ListRecommend",
//  })
    // invoke
    this.$.get(params[routers.type]).then(reshandler.success(success, fail, makeData), reshandler.error(fail))
}
